package hu.test.atacama;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class CommServiceReceiver extends BroadcastReceiver {

	String TAG = "CommServiceReceiver";

	@Override
	public void onReceive(Context context, Intent intent) {
		try {
			DataFromCloud dataFromCloud = (DataFromCloud) intent.getExtras()
					.getParcelable("dataFromCloud");
			int statusCode = intent.getExtras().getInt("statusCode");

			if (statusCode == 0) {
				dataFromCloud.setMessage(context
						.getString(R.string.message_no_connection));
			}
			Log.d(TAG,
					"dataFromCloud error: "
							+ Boolean.toString(dataFromCloud.isError()));
			if (!dataFromCloud.isError()) {
				Common common = Common.getInstance(context);
				if (dataFromCloud.getCommand().equals(
						Common.COMMAND_STORE_SERIALNUMBER)) {
					common.updateSharedPreferenceByKey(
							context.getString(R.string.key_serial_number),
							dataFromCloud.getSerialNumber());
				}
				if (dataFromCloud.getCommand().equals(
						Common.COMMAND_SHOW_SENSOR_VALUES)) {
					common.setCarLastKnownCoordinates(dataFromCloud.deviceInfo.get(0).getLatitude() + ',' +
							dataFromCloud.deviceInfo.get(0).getLongitude() + ',' +
							dataFromCloud.deviceInfo.get(0).getLastSeen());
					common.sensorList.clear();
					for (Sensor sensor : dataFromCloud.getSensorList()) {
						common.sensorList.add(new Sensor(sensor.getSensorName(), sensor
								.getSensorValue(), sensor.getUnitOfMeasure()));
					}
				}
				if (dataFromCloud.getCommand().equals(Common.COMMAND_ADDMARKER)) {
					Log.d(TAG, "COMMAND_ADDMARKER is ok");

					MapsInitializer.initialize(context);
					int i = 0;

					Log.d(TAG,
							"dataFromCloud.deviceInfo.size() - "
									+ Integer.toString(dataFromCloud.deviceInfo
											.size()));
					MarkerOptions[] markers = new MarkerOptions[dataFromCloud.deviceInfo
							.size()];
					for (DeviceInfo deviceInfo : dataFromCloud.deviceInfo) {
						Log.d(TAG, "markers loop");
						String title, snippet;
						title = "TESZT";
						snippet = deviceInfo.getOwnerName() + ";"
								+ deviceInfo.getLicensePlate() + ";"
								+ deviceInfo.getLastSeen() + ";"
								+ deviceInfo.getPictureURL() + ";"
								+ deviceInfo.getDeviceType() + ";"
								+ deviceInfo.getObjectKey();
						markers[i] = new MarkerOptions()
								.position(
										new LatLng(Double
												.parseDouble(deviceInfo
														.getLatitude()), Double
												.parseDouble(deviceInfo
														.getLongitude())))
								.title(title)
								.snippet(snippet)
								.icon(deviceInfo.getDeviceType().equals(
										"android") ? BitmapDescriptorFactory
										.fromResource(R.drawable.marker_phone)
										: BitmapDescriptorFactory
												.fromResource(R.drawable.marker_car));
						i++;
					}
					Log.d(TAG, "number of markers: " + Integer.toString(i));
					common.setMarkers(markers);

					showMap(context);
				}
				if (dataFromCloud.getCommand().equals(Common.COMMAND_SHOWMAP)) {
					common.setMarkers(null);
					showMap(context);
				}
				if (dataFromCloud.getCommand().equals(Common.COMMAND_SHOW_PATH)) {
					common.setPathData(dataFromCloud.getPathData());
				}
				if (dataFromCloud.getCommand().equals(
						Common.COMMAND_SHOWCARLIST)) {
					common.carList.clear();
					for (Car car : dataFromCloud.getCarList()) {
						common.carList.add(new Car(car.getCarKey(), car
								.getCarName()));
					}
					common.saveCarList();
				}
			}
			if (statusCode != 200) {
				Toast.makeText(
						context,
						context.getString(R.string.message_http_statuscode)
								+ statusCode, Toast.LENGTH_LONG).show();
			}
		} catch (Exception ex) {
			Toast.makeText(context,
					"CommServiceReceiver / onReceive: " + ex.toString(),
					Toast.LENGTH_LONG).show();
		}
	};

	private void showMap(Context context) {
		Intent mapIntent = new Intent();
		mapIntent
				.setClassName("hu.test.atacama", "hu.test.atacama.MapActivity");
		mapIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(mapIntent);
	}
}
